﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Net;

namespace OpenService.Service.Kuaidi100
{

    public sealed class QueryKuaidi100CodeActivity : CodeActivity
    {
        // 定义一个字符串类型的活动输入参数
        // http://api.kuaidi100.com/api?id=41fd72c59f1e3e26&com=yuantong&nu=6012248162&show=0&muti=1&order=desc
        // http://api.kuaidi100.com/api?id={0}&com={1}&nu={2}&show={3}&muti={4}&order={5}
        public InArgument<string> Address { get; set; }

        public InArgument<string> Key { get; set; }
        public InArgument<string> Com { get; set; }
        public InArgument<string> Nu { get; set; }
        public InArgument<string> Show { get; set; }
        public InArgument<string> Muti { get; set; }
        public InArgument<string> Order { get; set; }
        public OutArgument<string> Result { get; set; }

        // 如果活动返回值，则从 CodeActivity<TResult>
        // 派生并从 Execute 方法返回该值。
        protected override void Execute(CodeActivityContext context)
        {
            string id = context.GetValue(this.Key);
            string com = context.GetValue(this.Com);
            string nu = context.GetValue(this.Nu);
            string show = context.GetValue(this.Show);
            string muti = context.GetValue(this.Muti);
            string order = context.GetValue(this.Order);

            string address = string.Format(context.GetValue(this.Address), id, com, nu, show, muti, order);
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;
            string result = client.DownloadString(address);
            context.SetValue(this.Result, result);
        }
    }
}
